<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { loginByMobile, loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useSendMobileCode } from '@/composable'
import axios from 'axios'

const store = useUserStore()
const router = useRouter()
const route = useRoute()

// 1. 去注册
const reg = () => {
  console.log('去注册')
}

// 2. 表单业务
const mobile = ref('')
const password = ref('')
const isAgree = ref(false)

// 3. 登录操作
const login = async () => {
  // 3.1 判断是否同意用户协议
  if (!isAgree.value) {
    showToast('请勾选同意用户协议')
    return
  }
  // 3.2 调用登录接口
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  store.setUser(res.data)
  // 3.3 回跳页面(去个人中心 或者去传过来的地址对应的页面)
  router.push((route.query.returnUrl as string) || '/user')
  // 3.4 提示登录成功
  showSuccessToast('登录成功~')
}

// 4. 短信验证码操作
const isPass = ref(true)
const code = ref('')

const { time, formRef, send } = useSendMobileCode(mobile, 'login')

// 5. 密码框明文或密文显示
const show = ref(false)

// 6. 三方登录
onMounted(() => {
  // 测试mock接口
  axios.get('/patient/message/sys/list').then((res) => {
    console.log(res)
  })
})

const qqUrl = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${encodeURIComponent(
  import.meta.env.VITE_APP_CALLBACK + '/login/callback'
)}`
</script>

<template>
  <div class="login-page">
    <!-- 导航条 -->
    <cp-nav-bar title="登录" right-text="注册" @click-right="reg"></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 中间表单 -->
    <van-form ref="formRef" autocomplete="off" @submit="login">
      <van-field
        name="mobile"
        type="tel"
        v-model="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
      />
      <!-- 密码输入框 -->
      <van-field
        v-if="isPass"
        :type="show ? 'text' : 'password'"
        v-model="password"
        placeholder="请输入密码"
        :rules="passwordRules"
      >
        <template #button>
          <cp-icon
            :name="`login-eye-${show ? 'on' : 'off'}`"
            @click="show = !show"
          ></cp-icon>
        </template>
      </van-field>
      <!-- 短信验证码输入框 -->
      <van-field
        v-else
        type="text"
        v-model="code"
        :rules="codeRules"
        placeholder="请输入验证码"
      >
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">
            {{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}</span
          >
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button type="primary" block round native-type="submit"
          >登录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <!-- 离开登录页把登录成功后跳转的路径持久化到仓库 -->
      <a
        @click="store.setReturnUrl($route.query.returnUrl as string)"
        class="icon"
        :href="qqUrl"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
      <!-- <span id="qq"></span> -->
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
